import request from '@/sheep/request';
import UserApi from '@/sheep/api/member/user';
import sheep from '@/sheep';
import { computed } from 'vue';

const AuthUtil = {
  // 使用手机 + 密码登录
  login: (data) => {
    return request({
      url: '/member/auth/login',
      method: 'POST',
      data,
      custom: {
        showSuccess: true,
        loadingMsg: '登录中',
        successMsg: '登录成功',
      },
    });
  },
  // 使用手机 + 验证码登录
  smsLogin: (data) => {
    return request({
      url: '/member/auth/sms-login',
      method: 'POST',
      data,
      custom: {
        showSuccess: true,
        loadingMsg: '登录中',
        successMsg: '登录成功',
      },
    });
  },
  // 发送手机验证码
  sendSmsCode: (mobile, scene) => {
    return request({
      url: '/member/auth/send-sms-code',
      method: 'POST',
      data: {
        mobile,
        scene,
      },
      custom: {
        loadingMsg: '发送中',
        showSuccess: true,
        successMsg: '发送成功',
      },
    });
  },
  // 登出系统
  logout: () => {
    return request({
      url: '/member/auth/logout',
      method: 'POST',
    });
  },
  // 刷新令牌
  refreshToken: (refreshToken) => {
    return request({
      url: '/member/auth/refresh-token',
      method: 'POST',
      params: {
        refreshToken,
      },
      custom: {
        loading: false, // 不用加载中
        showError: false, // 不展示错误提示
      },
    });
  },
  // 社交授权的跳转
  socialAuthRedirect: (type, redirectUri) => {
    return request({
      url: '/member/auth/social-auth-redirect',
      method: 'GET',
      params: {
        type,
        redirectUri,
      },
      custom: {
        showSuccess: true,
        loadingMsg: '登陆中',
      },
    });
  },
  // 社交快捷登录
  socialLogin: (type, code, state) => {
    return request({
      url: '/member/auth/social-login',
      method: 'POST',
      data: {
        type,
        code,
        state,
      },
      custom: {
        showSuccess: true,
        loadingMsg: '登陆中',
      },
    });
  },
  // 微信小程序的一键登录
  weixinMiniAppLogin: (phoneCode, loginCode, state) => {
    return request({
      url: '/member/auth/weixin-mini-app-login',
      method: 'POST',
      data: {
        phoneCode,
        loginCode,
        state,
      },
      custom: {
        showSuccess: true,
        loadingMsg: '登录中',
        successMsg: '登录成功',
      },
    });
  },
  // 创建微信 JS SDK 初始化所需的签名
  createWeixinMpJsapiSignature: (url) => {
    return request({
      url: '/member/auth/create-weixin-jsapi-signature',
      method: 'POST',
      params: {
        url,
      },
      custom: {
        showError: false,
        showLoading: false,
      },
    });
  },
  //实名认证
  authProtocol1: async () => {
    let sh;
    try {
      // 登录且未注册时
      const isLogin = computed(() => sheep.$store('user').isLogin);
      if (isLogin.value) {
        // 已登录但未实名时
        const isName = await UserApi.isNameAuthenication();
        if (isName.data) {
          //已实名，判断用户等级
          const levelId = await UserApi.levelId(); //查看用户是否已经通过认证
          console.log('levelId', levelId.data);
          if (levelId.data) {
            //审核步骤
            console.log("aa");

            // const isGrant = await UserApi.isGrant(); //查看用户是否已经通过审核
            // if (!isGrant.data) {
            //   setTimeout(() => {
            //     sheep.$router.go('/pages/order/list');
            //     sheep.$helper.toast('该用户未审核，请检查订单状态');
            //   }, 1000);
            // }
          }
           else {
              sheep.$helper.toast('请先购买饮水机产品');
            setTimeout(() => {
              sheep.$router.go('/pages/register/category');
            }, 500);
          }
        } else if(isName.data == false) {
          sh = setTimeout(() => {
            sheep.$router.go('/pages/register/auth-identification'); // 跳转到实名认证页面
            clearInterval(sh);
          }, 500);
          sheep.$helper.toast('该用户未实名认证');
        }else{
          sheep.$helper.toast('无法获取请求，请联系服务器管理员');
        }
      } else if (isLogin.value == false) {
        // 未登录时跳到登录页
        sh = setTimeout(() => {
          sheep.$helper.toast('该用户未登录');
          sheep.$router.go('/pages/index/mainlogin'); // 跳转到实名认证页面
        }, 500);
      }else {
        sheep.$helper.toast('无法获取请求，请联系服务器管理员'); 
      }
      //  else {
      //   sh = setInterval(() => {
      //     sheep.$router.go('/pages/index/login'); // 跳转到微信登录页面
      //     // console.log(sheep.$router.getCurrentRoute().path);    // 打印当前路由
      //     if (sheep.$router.getCurrentRoute().path == '/pages/index/login') {
      //       clearInterval(sh);
      //     }
      //   }, 500);
      // }
    } catch (error) {
      sheep.$helper.toast('无法获取请求，请联系服务器管理员');
    }
  },
};

export default AuthUtil;
